package com.nowcoder.community.service;

import com.nowcoder.community.dao.MessageMapper;
import com.nowcoder.community.entity.Message;
import com.nowcoder.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * @ClassName MessageService
 * @Description
 * @Author hhwu
 * @Date 2022/5/24 10:36
 * Version 1.0
 **/
@Service
public class MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    public List<Message> findConversations(int userId, int offset, int limit) {
        /**
         * @Author hhwu
         * @Description //TODO 查询会话的信息。返回每一组会话中的最新的一条私信
         * @Date 10:42 2022/5/24
         * @Param [userId, offset, limit]
         * @return java.util.List<com.nowcoder.community.entity.Message>
         **/
        return messageMapper.selectConversations(userId, offset, limit);
    }
    
    public int findConversationCount(int userId) {
        /**
         * @Author hhwu
         * @Description //TODO 查询一共有几组会话
         * @Date 10:43 2022/5/24
         * @Param [userId]
         * @return int
         **/
        return messageMapper.selectConversationCount(userId);
    }
    
    public List<Message> findLetters(String conversationId, int offset, int limit) {
        /**
         * @Author hhwu
         * @Description //TODO 根据会话id查询每一组会话中所有的私信
         * @Date 10:45 2022/5/24
         * @Param [conversationId, offset, limit]
         * @return java.util.List<com.nowcoder.community.entity.Message>
         **/
        return messageMapper.selectLetters(conversationId, offset, limit);
    }
    
    public int findLetterCount(String conversationId) {
        /**
         * @Author hhwu
         * @Description //TODO 查询每一组会话中私信的数量
         * @Date 10:46 2022/5/24
         * @Param [conversationId]
         * @return int
         **/
        return messageMapper.selectLetterCount(conversationId);
    }
    
    public int findLetterUnreadCount(int userId, String conversationId) {
        /**
         * @Author hhwu
         * @Description //TODO 查询未读消息的数量。如果conversationId为空，则查询全部会话的未读私信数量，如果不为空则查询该条会话的未读私信数量
         * @Date 10:48 2022/5/24
         * @Param [userId, conversationId]
         * @return int
         **/
        return messageMapper.selectLetterUnreadCount(userId, conversationId);
    }

    public int addMessage(Message message) {
        /**
         * @Author hhwu
         * @Description //TODO 新建一条新的私信
         * @Date 15:15 2022/5/24
         * @Param [message]
         * @return int
         **/
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));// 过滤恶意注入和敏感词

        return messageMapper.insertMessage(message);
    }

    public int readMessage(List<Integer> ids) {
        /**
         * @Author hhwu
         * @Description //TODO 修改多条私信的状态为已读
         * @Date 15:16 2022/5/24
         * @Param [ids]
         * @return int
         **/
        return messageMapper.updateStatus(ids, 1);
    }

    public Message findLatestNotice(int userId, String topic) {
        /**
         * @Author hhwu
         * @Description //TODO 查询某topic的最新一条通知
         * @Date 16:37 2022/5/27
         * @Param [userId, topic]
         * @return com.nowcoder.community.entity.Message
         **/
        System.out.println(userId+":"+topic);
        return messageMapper.selectLatestNotice(userId, topic);
    }

    public int findNoticeCount(int userId, String topic) {
        /**
         * @Author hhwu
         * @Description //TODO 查询某topic的通知数量
         * @Date 16:37 2022/5/27
         * @Param [userId, topic]
         * @return int
         **/
        return messageMapper.selectNoticeCount(userId, topic);
    }

    public int findNoticeUnreadCount(int userId, String topic) {
        /**
         * @Author hhwu
         * @Description //TODO 查询某topic的未读通知数量
         * @Date 16:38 2022/5/27
         * @Param [userId, topic]
         * @return int
         **/
        return messageMapper.selectNoticeUnreadCount(userId, topic);
    }

    public List<Message> findNotices(int userId, String topic, int offset, int limit) {
        /**
         * @Author hhwu
         * @Description //TODO 查询某个主题下的所有通知，并进行分页处理
         * @Date 16:38 2022/5/28
         * @Param [userId, topic, offset, limit]
         * @return List
         **/
        return messageMapper.selectNotices(userId, topic, offset, limit);
    }


}
